#include <iostream>
#include "geometry.h"
#include "left_turn.h"

polygon::polygon()
{
   std::cin >> n;
   for (int i = 0; i < n; i++)
   {
      double x, y;
      std::cin >> x >> y;
      vertix.push_back(point(x, y));
   }
   vertix.push_back(vertix[0]);
   n++;
}
namespace
{
   bool between(point a, point b, point p)
   {
      if (p.y < a.y && p.y < b.y)
      {
         return false;
      }
      if (p.y > a.y && p.y > b.y)
      {
         return false;
      }
      return true;
   }
}

int polygon::in_polygon(point p)
{
   bool inter = false;
   for (int i = 0; i < n - 1; i++)
   {
      point a = vertix[i];
      point b = vertix[i + 1];
      if (a.x > b.x)
      {
         std::swap(a, b);
      }
      if (a.x > p.x || p.x > b.x)
      {
         continue;
      }
      int turn = left_turn(a, b, p);
      if (turn == 0 && between(a, b, p))
      {
         return 0;
      }
      if (turn == 1 && p.x < b.x)
      {
         inter = !inter;
      }
   }
   if (inter)
   {
      return 1;
   }
   return -1;
}